package csu.web.mypetstore.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import csu.web.mypetstore.mapper.InventoryMapper;
import csu.web.mypetstore.mapper.ItemMapper;
import csu.web.mypetstore.pojo.Inventory;
import csu.web.mypetstore.pojo.Item;
import csu.web.mypetstore.pojo.OrderDetail;
import csu.web.mypetstore.pojo.Product;
import csu.web.mypetstore.service.ItemService;
import csu.web.mypetstore.service.OrderDetailService;
import csu.web.mypetstore.service.ProductService;
import csu.web.mypetstore.utils.Result;
import csu.web.mypetstore.utils.ResultToJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ItemServiceImpl extends ServiceImpl<ItemMapper, Item>
        implements ItemService{

    @Autowired
    private ItemMapper itemMapper;
    @Autowired
    private InventoryMapper inventoryMapper;
    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    private ProductService productService;





    @Override
    public Item getItem(String itemId) {
        Wrapper<Item>wrapper=new LambdaQueryWrapper<Item>()
                .eq(Item::getItemid,itemId);
        return itemMapper.selectOne(wrapper);

    }

    @Override
    public List<Item> getItemListByProductId(String productId) {
        Wrapper<Item>wrapper=new LambdaQueryWrapper<Item>()
                .eq(Item::getProductid,productId);
        return itemMapper.selectList(wrapper);

    }

    @Override
    public boolean isItemInStock(String itemId) {

        Wrapper<Inventory>wrapper=new LambdaQueryWrapper<Inventory>()
                .eq(Inventory::getItemid,itemId);
        Inventory inventory = inventoryMapper.selectOne(wrapper);


        if (inventory == null) {
            return false;
        }
        if (inventory.getQty() > 0) {
            return true;
        }
        return false;
    }

    @Override
    public double getItemListPrice(String itemId) {
        Item item=getItem(itemId);
        return item.getListprice();
    }

    @Override
    public int getLeftItemNum(String itemId) {
        Wrapper<Inventory>wrapper=new LambdaQueryWrapper<Inventory>()
                .eq(Inventory::getItemid,itemId);
        Inventory inventory = inventoryMapper.selectOne(wrapper);
        return inventory != null ? inventory.getQty() : 0;
    }

    @Override
    public List<Item> getAllItem() {
        return itemMapper.selectList(null);
    }

    @Override
    public List<Inventory> getAllInventory() {
        return inventoryMapper.selectList(null);
    }

    @Override
    public void increaseItemNum(int num, String itemId) {
        Wrapper<Inventory>wrapper=new LambdaQueryWrapper<Inventory>()
                .eq(Inventory::getItemid,itemId);
        Inventory inventory = inventoryMapper.selectOne(wrapper);
        if (inventory != null) {
            inventory.setQty(inventory.getQty() + num);
            Wrapper<Inventory>wrapper1=new LambdaQueryWrapper<Inventory>()
                    .eq(Inventory::getItemid,itemId);
            inventoryMapper.update(inventory,wrapper1);
        }
    }

    @Override
    public void decreaseItemNum(int num, String itemId) {
        Wrapper<Inventory>wrapper=new LambdaQueryWrapper<Inventory>()
                .eq(Inventory::getItemid,itemId);
        Inventory inventory = inventoryMapper.selectOne(wrapper);
        if (inventory != null && inventory.getQty() >= num) {
            inventory.setQty(inventory.getQty() - num);
            Wrapper<Inventory>wrapper1=new LambdaQueryWrapper<Inventory>()
                    .eq(Inventory::getItemid,itemId);
            inventoryMapper.update(inventory,wrapper1);
        }
    }

    @Override
    public void removeItem(String itemId) {
        Wrapper<Item>wrapper=new LambdaQueryWrapper<Item>()
                .eq(Item::getItemid,itemId);
        itemMapper.delete(wrapper);
        Wrapper<Inventory>wrapper1=new LambdaQueryWrapper<Inventory>()
                .eq(Inventory::getItemid,itemId);
        inventoryMapper.delete(wrapper1);
    }

    @Override
    public void updateInventory(String itemId,String newId, int quantity) {
        Wrapper<Inventory>wrapper=new LambdaQueryWrapper<Inventory>()
                .eq(Inventory::getItemid,itemId);
        Inventory inventory = inventoryMapper.selectOne(wrapper);
        inventory.setItemid(newId);
        inventory.setQty(quantity);
        inventoryMapper.update(inventory,wrapper);
    }

    @Override
    public void moveItemToProduct(String itemId, String productId) {
        Item item=getItem(itemId);
        if (item != null) {
            item.setProductid(productId);
            itemMapper.updateById(item);
        }
    }

    @Override
    public void deleteItemInProduct(String productId) {
        Wrapper<Item>wrapper=new LambdaQueryWrapper<Item>()
                .eq(Item::getProductid,productId);
        itemMapper.delete(wrapper);
    }

    @Override
    public void insertItem(Item item,Integer qty) {
        itemMapper.insert(item);

       Inventory inventory=new Inventory();
       inventory.setItemid(item.getItemid());
       inventory.setQty(qty);
       inventoryMapper.insert(inventory);


    }

    @Override
    public void updateItem(Item item) {
        Wrapper<Item>wrapper=new LambdaQueryWrapper<Item>()
                .eq(Item::getId,item.getId());
        itemMapper.update(item,wrapper);

    }

    @Override
    public void decreaseItemInventoryInOrder(int oderId) {
        List<OrderDetail>orderDetailList=orderDetailService.queryOrderDetail(oderId);

        for(OrderDetail orderDetail:orderDetailList){
            decreaseItemNum(orderDetail.getQuantity(),orderDetail.getItemid());
        }
    }

    @Override
    public double itemPrice(Item item, int quantity) {
        return  (item.getListprice() * quantity);
    }

    @Override
    public boolean isItemInStack(String itemId, int quantity) {
        Wrapper<Inventory>wrapper=new LambdaQueryWrapper<Inventory>()
                .eq(Inventory::getItemid,itemId);
        Inventory inventory = inventoryMapper.selectOne(wrapper);
        if(inventory!=null && inventory.getQty()<quantity)
        {
            return false;
        }


        return true;
    }

    @Override
    public Result getItemInfo(String itemId) {
        Item item=getItem(itemId);
        System.out.println(item.getProductid());
        Inventory inventory=inventoryMapper.selectOne(new LambdaQueryWrapper<Inventory>().eq(Inventory::getItemid,itemId));

        Product product=productService.getProduct(item.getProductid());

        Map<String,Object> map=new HashMap<>();
        map.put("image",product.getImage());
        map.put("description",product.getDescription());
        map.put("itemId",item.getItemid());
        map.put("name",product.getName());
        map.put("quantity",inventory.getQty());
        map.put("price",item.getListprice());
        ResultToJson.convertResultToJson(Result.success(map));
        return Result.success(map);
    }

    @Override
    public Result getItemList(String productId) {
        Map<String,Object> map=new HashMap<>();


         List<Item> itemList=getItemListByProductId(productId);
         map.put("itemList",itemList);
         Product product=productService.getProduct(productId);
         map.put("product",product);

         return Result.success(map);

    }

}




